import { useState } from 'react';
import { PaginateReturn } from '../types';

export function useFetchCategory<T = any, U = any>(request: <T, U>(params?: T) => Promise<U>) {
    const [data, setData] = useState<U>([] as U);
    const [total, setTotal] = useState(0);
    const fetchData = async (params?: T) => {
        try {
            const res = await request<T, U>(params);
            setTotal(
                (res as PaginateReturn<any>).paginateMeta &&
                    (res as PaginateReturn<any>).paginateMeta.total,
            );
            setData(res);
        } catch (error) {
            console.log(error);
        }
    };
    return {
        data,
        fetchData,
        total,
    };
}
